import { _decorator } from 'cc';
import { EntiyManager } from '../../common/EnityManager';
import { IEntity } from '../../Levels';
import { TILE_HEIGHT, TILE_WEIGHT } from '../Tile/TileItem';
import { DIRECTION_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM } from '../../Enum';
import { EventManager } from '../../RunTime/EventManager';
import { DoorStateMachine } from './DoorStateMachine';
import { DataManager } from '../../RunTime/DataManager';
const { ccclass, property } = _decorator;


@ccclass('DoorManager')
export class DoorManager extends EntiyManager {


  async init(param: IEntity){
    this.fsm = this.addComponent(DoorStateMachine)
    await this.fsm.init()
    super.init(param)
    this.node.setPosition(this.x * TILE_WEIGHT - 1.5*TILE_WEIGHT, -this.y * TILE_HEIGHT + 1.5*TILE_HEIGHT)
    EventManager.getInstance().on(EVENT_ENUM.ENEMY_DEAD, this.OnEnemyDead, this)
  }

  protected onDestroy(): void {
    EventManager.getInstance().off(EVENT_ENUM.ENEMY_DEAD, this.OnEnemyDead)
  }

  OnEnemyDead(){
    console.log(DataManager.getInstance().enemys.map(enemy => enemy.state))
    const allDead = DataManager.getInstance().enemys.every(enemy => enemy.state == ENTITY_STATE_ENUM.DEAD)
    if (allDead) {
      this.state = ENTITY_STATE_ENUM.DEAD
    }
  }


}
